package com.xmhmyh.generalframework.common.wxutils.Handlers;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.xmhmyh.generalframework.common.wxutils.entity.WxJsApiResult;
import com.xmhmyh.generalframework.common.wxutils.entity.WxLoginResult;
import com.xmhmyh.generalframework.common.wxutils.entity.WxTokenResult;
import com.xmhmyh.generalframework.common.wxutils.entity.WxUserInfoResult;
import com.xmhmyh.generalframework.common.wxutils.util.HttpUtil;
import com.xmhmyh.generalframework.common.wxutils.util.WxUrl;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 于丁
 * 微信用户相关处理
 * @DateTime: 2020/4/20 1:59 下午
 */
public class WxUserHandler extends WxHandler {

    /**
     * @Author: 于丁
     * @Description: 小程序登录
     * @DateTime: 2020/4/20 2:23 下午
     * @Params:
     * @Return
     */
    public WxLoginResult miniAppLogin(String appId, String appSecret, String code) {
        try {
            String result = HttpUtil.get(WxUrl.getJscode2sessionUrl(appId, appSecret, code));
            WxLoginResult wxLoginResult = JSONObject.parseObject(result, WxLoginResult.class);
            return wxLoginResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Author: 于丁
     * @Description: 公众号登录
     * @DateTime: 2020/5/21 11:00 上午
     * @Params:
     * @Return
     */
    public WxLoginResult gzhLogin(String appId, String appSecret, String code) {
        try {
            String result = HttpUtil.get(WxUrl.getGzhLoginUrl(appId, appSecret, code));
            WxLoginResult wxLoginResult = JSONObject.parseObject(result, WxLoginResult.class);
            return wxLoginResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成总Token
     *
     * @return
     */
    public WxTokenResult getWxSDKAccessToken(String appId, String appSecret) {
        try {
            String result = HttpUtil.get(WxUrl.getWxAccessToken(appId, appSecret));
            System.out.println("获取WxSdkaccessToken:" + result);
            WxTokenResult wxTokenResult = JSONObject.parseObject(result, WxTokenResult.class);
            return wxTokenResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public WxUserInfoResult getUserInfo(String accessToken, String openId) {
        try {
            String result = HttpUtil.get(WxUrl.getWeixinUserInfoByOpenId(accessToken,openId));
            WxUserInfoResult wxUserInfoResult = JSONObject.parseObject(result, WxUserInfoResult.class);
            return wxUserInfoResult;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 签名jsapi
     *
     * @param url 需要签名的url
     * @return
     * @throws Exception
     */
    public Map<String, Object> signJsApi(String appId, String url, String accessToken, String nonce_str) {
        try {
            String result = HttpUtil.get(WxUrl.getJSAPI_TICKET(accessToken));
            System.out.println("====================获取到的+"+result+"+===================");
            WxJsApiResult wxJsApi = JSONObject.parseObject(result, WxJsApiResult.class);
            String timestamp = Long.toString(System.currentTimeMillis() / 1000L);
            String signatureStr = "jsapi_ticket=" + wxJsApi.getTicket() +
                    "&noncestr=" + nonce_str +
                    "&timestamp=" + timestamp +
                    "&url=" + url;
            String signature = SecureUtil.sha1(signatureStr);
            Map<String, Object> apiSign = new HashMap<>();
            apiSign.put("jsapi_ticket", wxJsApi.getTicket());
            apiSign.put("appid", appId);
            apiSign.put("timestamp", timestamp);
            apiSign.put("nonceStr", nonce_str);
            apiSign.put("signature", signature);
            return apiSign;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
